<?php

class Content_AdminPostController extends Zend_Controller_Action
{

	public function init()
	{

	}

	public function indexAction()
	{
		$table = Nuo::getTable('content.post');

		$select = $table -> select();

		$this -> view -> paginator = $paginator = Zend_Paginator::factory($select);

		$paginator -> setCurrentPageNumber($this -> _getParam('page', 1));
	}

	/**
	 * add post
	 */
	public function addAction()
	{
		$form = $this -> view -> form = new Content_Form_Admin_Post_Add;

		/**
		 * @var Content_Model_DbTable_Posts
		 */
		$table = Nuo::getTable('content.post');

		/**
		 * @var User_Model_User
		 */
		$viewer = Nuo::getViewer();

		/**
		 * @var Zend_Controller_Request_Http
		 */
		$req = $this -> getRequest();

		/**
		 * get post id
		 * @var int
		 */
		$post_id = $req -> getParam('post_id', 0);

		/**
		 * @var Content_Model_Post
		 */
		$item = null;

		if ($post_id)
		{
			$item = $table -> find($post_id) -> current();
		}

		/**
		 * Is GET
		 */
		if ($req -> isGet())
		{
			if (is_object($item))
			{
				$form -> populate($item -> toArray());
			}
			return;
		}

		/**
		 * is HTTP POST
		 */
		if ($req -> isPost() && $form -> isValid($req -> getPost()))
		{
			$values = $form -> getValues();
			$item = $table -> fetchNew();
			$item -> setFromArray($values);

			/**
			 * set created time
			 */
			$item -> created_date = $item -> updated_date = Nuo::getDatetime();

			/**
			 * set created by
			 */
			$item -> updated_by = $item -> created_by = $viewer -> getId();

			if (empty($item -> post_author))
			{
				$item -> post_author = $viewer -> getTitle();
			}

			/**
			 * save
			 */
			$item -> save();

			/**
			 * redirect to content home page
			 */
			$url = Zend_Controller_Front::getInstance() -> getRouter() -> assemble(array(
					'module' => 'content',
					'controller' => 'post',
					'action' => 'index'
			), 'admin', true);
			$this -> _helper -> redirector -> gotoUrl($url, array('prependBase' => 0));
		}
	}

	/**
	 * edit post
	 */
	public function editAction()
	{
		$form = $this -> view -> form = new Content_Form_Admin_Post_Edit;

		/**
		 * @var Content_Model_DbTable_Posts
		 */
		$table = Nuo::getTable('content.post');

		/**
		 * @var User_Model_User
		 */
		$viewer = Nuo::getViewer();

		/**
		 * @var Zend_Controller_Request_Http
		 */
		$req = $this -> getRequest();

		/**
		 * get post id
		 * @var int
		 */
		$post_id = $req -> getParam('post_id', 0);

		/**
		 * @var Content_Model_Post
		 */
		$item = $table -> find($post_id) -> current();

		/**
		 * Is GET
		 */
		if ($req -> isGet())
		{
			if (is_object($item))
			{
				$form -> populate($item -> toArray());
			}
			return;
		}

		/**
		 * is Post
		 */
		if ($req -> isPost() && $form -> isValid($req -> getPost()))
		{
			$values = $form -> getValues();

			if (!is_object($item))
			{
				$item = $table -> fetchNew();
				/**
				 * set created time
				 */
				$item -> created_date = $item -> updated_date = Nuo::getDatetime();

				/**
				 * set created by
				 */
				$item -> updated_by = $item -> created_by = $viewer -> getId();
			}

			$item -> setFromArray($values);
			/**
			 * set created time
			 */
			$item -> updated_date = Nuo::getDatetime();

			/**
			 * set created by
			 */
			$item -> updated_by = $viewer -> getId();

			/**
			 * commit track
			 */
			$item -> save();

			/**
			 * redirect to content home page
			 */
			$url = Zend_Controller_Front::getInstance() -> getRouter() -> assemble(array(
					'module' => 'content',
					'controller' => 'post',
					'action' => 'index'
			), 'admin', true);

			$this -> _helper -> redirector -> gotoUrl($url, array('prependBase' => 0));
		}

	}

	/**
	 * edit post
	 */
	public function deleteAction()
	{
		$form = $this -> view -> form = new Content_Form_Admin_Post_Delete;
		
		/**
		 * @var Content_Model_DbTable_Posts
		 */
		$table = Nuo::getTable('content.post');

		/**
		 * @var User_Model_User
		 */
		$viewer = Nuo::getViewer();

		/**
		 * @var Zend_Controller_Request_Http
		 */
		$req = $this -> getRequest();

		/**
		 * get post id
		 * @var int
		 */
		$post_id = $req -> getParam('post_id', 0);

		/**
		 * @var Content_Model_Post
		 */
		$item = $table -> find($post_id) -> current();

		/**
		 * Is GET
		 */
		if ($req -> isGet())
		{
			if (is_object($item))
			{
				$form -> populate($item -> toArray());
			}
			return;
		}

		/**
		 * is Post
		 */
		if ($req -> isPost() && $form -> isValid($req -> getPost()))
		{
			$values = $form -> getValues();

			if (is_object($item))
			{
				$item->delete();
			}

			/**
			 * redirect to content home page
			 */
			$url = Zend_Controller_Front::getInstance() -> getRouter() -> assemble(array(
					'module' => 'content',
					'controller' => 'post',
					'action' => 'index'
			), 'admin', true);

			$this -> _helper -> redirector -> gotoUrl($url, array('prependBase' => 0));
		}

	}

}
